package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.BrowsingHistory;
import com.atguigu.lease.web.app.mapper.BrowsingHistoryMapper;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.vo.history.HistoryItemVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author liubo
 * @description 针对表【browsing_history(浏览历史)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class BrowsingHistoryServiceImpl extends ServiceImpl<BrowsingHistoryMapper, BrowsingHistory>
        implements BrowsingHistoryService {

    @Autowired
    private BrowsingHistoryMapper mapper;

    /**
     * 分页查询获取浏览历史
     *
     * @param page
     * @param userId
     * @return
     */
    @Override
    public IPage<HistoryItemVo> pageHistory(IPage<HistoryItemVo> page, Long userId) {
        return mapper.pageHistory(page, userId);

    }

    /**
     * 根据用户id,浏览该房间的id保存浏览历史
     *
     * @param userId
     * @param roomId
     */
    @Override
    @Async
    public void saveHistory(Long userId, Long roomId) {
        LambdaQueryWrapper<BrowsingHistory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BrowsingHistory::getUserId, userId);
        wrapper.eq(BrowsingHistory::getRoomId, roomId);
        BrowsingHistory browsingHistory = mapper.selectOne(wrapper);
        if (browsingHistory != null) {
            //更新
            browsingHistory.setBrowseTime(new Date());
            mapper.updateById(browsingHistory);
        } else {
            BrowsingHistory History = new BrowsingHistory();
            History.setRoomId(roomId);
            History.setUserId(userId);
            History.setBrowseTime(new Date());
            mapper.insert(History);
        }
    }
}